{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instant</th>\n",
       "      <th>dteday</th>\n",
       "      <th>season</th>\n",
       "      <th>yr</th>\n",
       "      <th>mnth</th>\n",
       "      <th>holiday</th>\n",
       "      <th>weekday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>weathersit</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>casual</th>\n",
       "      <th>registered</th>\n",
       "      <th>cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2011-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.344167</td>\n",
       "      <td>0.363625</td>\n",
       "      <td>0.805833</td>\n",
       "      <td>0.160446</td>\n",
       "      <td>331</td>\n",
       "      <td>654</td>\n",
       "      <td>985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>2011-01-02</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.363478</td>\n",
       "      <td>0.353739</td>\n",
       "      <td>0.696087</td>\n",
       "      <td>0.248539</td>\n",
       "      <td>131</td>\n",
       "      <td>670</td>\n",
       "      <td>801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>2011-01-03</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.196364</td>\n",
       "      <td>0.189405</td>\n",
       "      <td>0.437273</td>\n",
       "      <td>0.248309</td>\n",
       "      <td>120</td>\n",
       "      <td>1229</td>\n",
       "      <td>1349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>2011-01-04</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.212122</td>\n",
       "      <td>0.590435</td>\n",
       "      <td>0.160296</td>\n",
       "      <td>108</td>\n",
       "      <td>1454</td>\n",
       "      <td>1562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>2011-01-05</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.226957</td>\n",
       "      <td>0.229270</td>\n",
       "      <td>0.436957</td>\n",
       "      <td>0.186900</td>\n",
       "      <td>82</td>\n",
       "      <td>1518</td>\n",
       "      <td>1600</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   instant      dteday  season  yr  mnth  holiday  weekday  workingday  \\\n",
       "0        1  2011-01-01       1   0     1        0        6           0   \n",
       "1        2  2011-01-02       1   0     1        0        0           0   \n",
       "2        3  2011-01-03       1   0     1        0        1           1   \n",
       "3        4  2011-01-04       1   0     1        0        2           1   \n",
       "4        5  2011-01-05       1   0     1        0        3           1   \n",
       "\n",
       "   weathersit      temp     atemp       hum  windspeed  casual  registered  \\\n",
       "0           2  0.344167  0.363625  0.805833   0.160446     331         654   \n",
       "1           2  0.363478  0.353739  0.696087   0.248539     131         670   \n",
       "2           1  0.196364  0.189405  0.437273   0.248309     120        1229   \n",
       "3           1  0.200000  0.212122  0.590435   0.160296     108        1454   \n",
       "4           1  0.226957  0.229270  0.436957   0.186900      82        1518   \n",
       "\n",
       "    cnt  \n",
       "0   985  \n",
       "1   801  \n",
       "2  1349  \n",
       "3  1562  \n",
       "4  1600  "
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "from sklearn.metrics import r2_score\n",
    "\n",
    "train = pd.read_csv('day.csv')\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(731, 15)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#数据准备\n",
    "import datetime\n",
    "\n",
    "train_data = pd.to_datetime(train['dteday'])\n",
    "train['dayofyear'] = train_data.dt.dayofyear \n",
    "\n",
    "#分离输入特征和输出\n",
    "y = train['cnt'].values\n",
    "X = train.drop('cnt', axis = 1)\n",
    "X = X.drop('dteday', axis = 1)\n",
    "\n",
    "columns = X.columns\n",
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instant</th>\n",
       "      <th>season</th>\n",
       "      <th>yr</th>\n",
       "      <th>mnth</th>\n",
       "      <th>holiday</th>\n",
       "      <th>weekday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>weathersit</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>casual</th>\n",
       "      <th>registered</th>\n",
       "      <th>dayofyear</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>726</th>\n",
       "      <td>727</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.254167</td>\n",
       "      <td>0.226642</td>\n",
       "      <td>0.652917</td>\n",
       "      <td>0.350133</td>\n",
       "      <td>247</td>\n",
       "      <td>1867</td>\n",
       "      <td>362</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>727</th>\n",
       "      <td>728</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.253333</td>\n",
       "      <td>0.255046</td>\n",
       "      <td>0.590000</td>\n",
       "      <td>0.155471</td>\n",
       "      <td>644</td>\n",
       "      <td>2451</td>\n",
       "      <td>363</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>728</th>\n",
       "      <td>729</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.253333</td>\n",
       "      <td>0.242400</td>\n",
       "      <td>0.752917</td>\n",
       "      <td>0.124383</td>\n",
       "      <td>159</td>\n",
       "      <td>1182</td>\n",
       "      <td>364</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>729</th>\n",
       "      <td>730</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.255833</td>\n",
       "      <td>0.231700</td>\n",
       "      <td>0.483333</td>\n",
       "      <td>0.350754</td>\n",
       "      <td>364</td>\n",
       "      <td>1432</td>\n",
       "      <td>365</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>730</th>\n",
       "      <td>731</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.215833</td>\n",
       "      <td>0.223487</td>\n",
       "      <td>0.577500</td>\n",
       "      <td>0.154846</td>\n",
       "      <td>439</td>\n",
       "      <td>2290</td>\n",
       "      <td>366</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     instant  season  yr  mnth  holiday  weekday  workingday  weathersit  \\\n",
       "726      727       1   1    12        0        4           1           2   \n",
       "727      728       1   1    12        0        5           1           2   \n",
       "728      729       1   1    12        0        6           0           2   \n",
       "729      730       1   1    12        0        0           0           1   \n",
       "730      731       1   1    12        0        1           1           2   \n",
       "\n",
       "         temp     atemp       hum  windspeed  casual  registered  dayofyear  \n",
       "726  0.254167  0.226642  0.652917   0.350133     247        1867        362  \n",
       "727  0.253333  0.255046  0.590000   0.155471     644        2451        363  \n",
       "728  0.253333  0.242400  0.752917   0.124383     159        1182        364  \n",
       "729  0.255833  0.231700  0.483333   0.350754     364        1432        365  \n",
       "730  0.215833  0.223487  0.577500   0.154846     439        2290        366  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(584, 15)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#分割80%作为训练数据 20%作为测试数据\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=33, test_size = .2)\n",
    "X_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py:475: DataConversionWarning: Data with input dtype int64 was converted to float64 by StandardScaler.\n",
      "  warnings.warn(msg, DataConversionWarning)\n"
     ]
    }
   ],
   "source": [
    "#数据标准化\n",
    "X_ss = StandardScaler()\n",
    "y_ss = StandardScaler()\n",
    "\n",
    "X_train = X_ss.fit_transform(X_train)\n",
    "X_test = X_ss.transform(X_test)\n",
    "\n",
    "y_train = y_ss.fit_transform(y_train.reshape(-1, 1))\n",
    "y_test = y_ss.transform(y_test.reshape(-1, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#最小二乘线性回归模型\n",
    "from sklearn.linear_model import LinearRegression\n",
    "\n",
    "#初始化模型\n",
    "lr = LinearRegression()\n",
    "\n",
    "# 训练\n",
    "lr.fit(X_train, y_train)\n",
    "\n",
    "# 预测\n",
    "y_test_predict_lr = lr.predict(X_test)\n",
    "y_train_predict_lr = lr.predict(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alpha of RidgeCV is: 0.01\n"
     ]
    }
   ],
   "source": [
    "#岭回归模型\n",
    "from sklearn.linear_model import RidgeCV\n",
    "\n",
    "alphas = [ 0.01, 0.1, 1, 10,100]\n",
    "\n",
    "#初始化模型\n",
    "ridge = RidgeCV(alphas = alphas, store_cv_values=True)\n",
    "\n",
    "# 训练\n",
    "ridge.fit(X_train, y_train)\n",
    "\n",
    "# 预测\n",
    "y_test_predict_ridge = ridge.predict(X_test)\n",
    "y_train_predict_ridge = ridge.predict(X_train)\n",
    "\n",
    "print(\"alpha of RidgeCV is:\", ridge.alpha_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alpha of LassoCV is: 0.0009464126228561161\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/coordinate_descent.py:1094: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
      "  y = column_or_1d(y, warn=True)\n"
     ]
    }
   ],
   "source": [
    "#Lasso模型\n",
    "from sklearn.linear_model import LassoCV\n",
    "\n",
    "#初始化模型\n",
    "lasso = LassoCV()\n",
    "\n",
    "# 训练\n",
    "lasso.fit(X_train, y_train)\n",
    "\n",
    "# 预测\n",
    "y_test_predict_lasso = lasso.predict(X_test)\n",
    "y_train_predict_lasso = lasso.predict(X_train)\n",
    "\n",
    "print(\"alpha of LassoCV is:\", lasso.alpha_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "r2 score of LinearRegression on test 1.0\n",
      "r2 score of LinearRegression on train 1.0\n",
      "r2 score of RidgeCV on test 0.9999999990521148\n",
      "r2 score of RidgeCV on train 0.9999999989650419\n",
      "r2 score of LassoCV on test 0.999998653262408\n",
      "r2 score of LassoCV on train 0.9999987021953919\n"
     ]
    }
   ],
   "source": [
    "#各模型在测试集上的性能\n",
    "print(\"r2 score of LinearRegression on test\", r2_score(y_test, y_test_predict_lr))\n",
    "print(\"r2 score of LinearRegression on train\", r2_score(y_train, y_train_predict_lr))\n",
    "\n",
    "print(\"r2 score of RidgeCV on test\", r2_score(y_test, y_test_predict_ridge))\n",
    "print(\"r2 score of RidgeCV on train\", r2_score(y_train, y_train_predict_ridge))\n",
    "\n",
    "print(\"r2 score of LassoCV on test\", r2_score(y_test, y_test_predict_lasso))\n",
    "print(\"r2 score of LassoCV on train\", r2_score(y_train, y_train_predict_lasso))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "      <th>coef_lasso</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>registered</td>\n",
       "      <td>[0.808424582331761]</td>\n",
       "      <td>[0.8083499708361581]</td>\n",
       "      <td>0.807749</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>casual</td>\n",
       "      <td>[0.35120342627366147]</td>\n",
       "      <td>[0.35120304211889386]</td>\n",
       "      <td>0.350514</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>dayofyear</td>\n",
       "      <td>[6.275235718465345e-16]</td>\n",
       "      <td>[-4.010658928987887e-05]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>temp</td>\n",
       "      <td>[4.675405650565998e-16]</td>\n",
       "      <td>[8.453343201930696e-06]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>yr</td>\n",
       "      <td>[2.4832247926496367e-16]</td>\n",
       "      <td>[3.426428072519916e-05]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>windspeed</td>\n",
       "      <td>[1.5043028903484545e-16]</td>\n",
       "      <td>[-5.71011986028741e-06]</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>instant</td>\n",
       "      <td>[8.998837020328263e-17]</td>\n",
       "      <td>[9.477106608424268e-06]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>workingday</td>\n",
       "      <td>[3.164300999432909e-17]</td>\n",
       "      <td>[2.1833937647097734e-05]</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>weathersit</td>\n",
       "      <td>[2.0901531385802765e-17]</td>\n",
       "      <td>[-1.4584325674809814e-05]</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>weekday</td>\n",
       "      <td>[-3.5623281588333045e-17]</td>\n",
       "      <td>[4.576113130108639e-06]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>holiday</td>\n",
       "      <td>[-6.597492418188709e-17]</td>\n",
       "      <td>[-6.482111687515502e-07]</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>atemp</td>\n",
       "      <td>[-1.4771602448878447e-16]</td>\n",
       "      <td>[1.9025730628077775e-05]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>hum</td>\n",
       "      <td>[-1.7846253758740037e-16]</td>\n",
       "      <td>[-2.9760365109661457e-06]</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>season</td>\n",
       "      <td>[-3.566523079142529e-16]</td>\n",
       "      <td>[2.4665251455252557e-05]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>mnth</td>\n",
       "      <td>[-4.040306718504724e-16]</td>\n",
       "      <td>[3.0120799282107313e-05]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       columns                    coef_lr                 coef_ridge  \\\n",
       "13  registered        [0.808424582331761]       [0.8083499708361581]   \n",
       "12      casual      [0.35120342627366147]      [0.35120304211889386]   \n",
       "14   dayofyear    [6.275235718465345e-16]   [-4.010658928987887e-05]   \n",
       "8         temp    [4.675405650565998e-16]    [8.453343201930696e-06]   \n",
       "2           yr   [2.4832247926496367e-16]    [3.426428072519916e-05]   \n",
       "11   windspeed   [1.5043028903484545e-16]    [-5.71011986028741e-06]   \n",
       "0      instant    [8.998837020328263e-17]    [9.477106608424268e-06]   \n",
       "6   workingday    [3.164300999432909e-17]   [2.1833937647097734e-05]   \n",
       "7   weathersit   [2.0901531385802765e-17]  [-1.4584325674809814e-05]   \n",
       "5      weekday  [-3.5623281588333045e-17]    [4.576113130108639e-06]   \n",
       "4      holiday   [-6.597492418188709e-17]   [-6.482111687515502e-07]   \n",
       "9        atemp  [-1.4771602448878447e-16]   [1.9025730628077775e-05]   \n",
       "10         hum  [-1.7846253758740037e-16]  [-2.9760365109661457e-06]   \n",
       "1       season   [-3.566523079142529e-16]   [2.4665251455252557e-05]   \n",
       "3         mnth   [-4.040306718504724e-16]   [3.0120799282107313e-05]   \n",
       "\n",
       "    coef_lasso  \n",
       "13    0.807749  \n",
       "12    0.350514  \n",
       "14    0.000000  \n",
       "8     0.000000  \n",
       "2     0.000000  \n",
       "11   -0.000000  \n",
       "0     0.000000  \n",
       "6    -0.000000  \n",
       "7    -0.000000  \n",
       "5     0.000000  \n",
       "4    -0.000000  \n",
       "9     0.000000  \n",
       "10   -0.000000  \n",
       "1     0.000000  \n",
       "3     0.000000  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#比较用上述三种模型得到的各特征的系数，\n",
    "ds = pd.DataFrame({\"columns\":list(columns),\"coef_lr\":list(lr.coef_.T),\"coef_ridge\":list(ridge.coef_.T),\"coef_lasso\":list(lasso.coef_.T)})\n",
    "ds.sort_values( by = ['coef_lr'], ascending = False )"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
